/**
 * Created by sxwu on 14-2-14.
 */
 /*
 * User model
 */



/**
 * 模块依赖
 */
var db = require("../lib/db");

//查找用户记录

exports.selectForLogin = function(uname, handler) {
    db.execQuery({
        "sql": "SELECT * FROM viruser WHERE username=?",
        "args": [uname],
        "handler": handler
    });

};
exports.selectByID = function(id, handler) {
    db.execQuery({
        "sql": "SELECT * FROM viruser WHERE id=?",
        "args": [id],
        "handler": handler
    });
};

exports.selectByIDForUserinfo = function(id, handler) {
    db.execQuery({
        "sql": "SELECT b.*, a.* FROM viruser a, roles b WHERE a.id=? and a.roleid=b.id",
        "args": [id],
        "handler": handler
    });
};

exports.updateAllInfo = function(users, handler) {
    db.execQuery({
        "sql": "update viruser set userpwd=?," +
            "nickname=?,email=?,phonenumber=?,companyname=? where id=?",
        "args":[
            users.userpwd,
            users.nickname,
            users.email,
            users.phonenumber,
            users.companyname,
            users.id],
        "handler": handler
    });
};

exports.updateLoginInfo = function(users, handler) {
    db.execQuery({
        "sql": "update viruser set lastlogindate=now()," +
            "lastloginip=?  where id=?",
        "args":[
            users.lastloginip,
            users.id],
        "handler": handler
    });
};

exports.selectByRoleType = function(roleid, handler) {
    db.execQuery({
        "sql": "SELECT b.*,a.* FROM viruser a, roles b WHERE a.roleid=b.id and a.proxyid=?",
        "args": [roleid],
        "handler": handler
    });
};

exports.selectAll = function( handler) {
    db.execQuery({
        "sql": "SELECT b.*,a.* FROM viruser a, roles b WHERE a.roleid=b.id",
        "handler": handler
    });
};

exports.selectForNotify = function( handler) {
    db.execQuery({
        "sql": "SELECT * FROM viruser",
        "handler": handler
    });
};

exports.deleteByID = function(id, handler) {
    db.execQuery({
        "sql": "delete FROM viruser WHERE  id = ?",
        "args": [id],
        "handler": handler
    });
};

exports.insert = function(users, handler) {
    db.execQuery({
        "sql": "insert into viruser " +
            "(username,userpwd,nickname,roleid,userflag,email,phonenumber,companyname,regdate,expiredate,proxyid) " +
            "values(?,?,?,?,?,?,?,?,now(),?,?)",
        "args":[ users.username,
            users.userpwd,
            users.nickname,
            users.roleid,
            users.userflag,
            users.email,
            users.phonenumber,
            users.companyname,
            users.expiredate,
            users.proxyid],
        "handler": handler
    });
};

////////////////////////////////////////////////

exports.update = function(users, handler) {
    db.execQuery({
        "sql": "update viruser set username=?,userpwd=?," +
            "nickname=?,userflag=?,email=?,phonenumber=?,companyname=?,roleid=?,expiredate=? where id=?",
        "args":[ users.username,
            users.userpwd,
            users.nickname,
            users.userflag,
            users.email,
            users.phonenumber,
            users.companyname,
            users.roleid,
            users.expiredate,
            users.id],
        "handler": handler
    });
};
